package com.glory.dgcpcpda.activity.check;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.mfg.DAWorkBeginActivity;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.CheckSheetEntity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.MLotToolEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class CheckChangeProductActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvState)
    TextView tvState;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvCheckTime)
    TextView tvCheckTime;
    @BindView(R.id.tvClazz)
    TextView tvClazz;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvCheckName)
    TextView tvCheckName;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private CheckSheetEntity entity = new CheckSheetEntity();
    private String mObjrctRRN = "";
    private static final String Start  = "Start";
    private static final String EqpSubStateMor  = "MOR";
    private Boolean IsStart = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_xxnew_check_table;
    }

    @Override
    protected void initView() {
        tvTitle.setText("换产点检");
        tvBaseSubmit.setText("下一步");
        tvState.setVisibility(View.VISIBLE);
        setMainLayout(llMain);
        String eqpid = getIntent().getExtras().getString("EQPID");
        mObjrctRRN = getIntent().getExtras().getString("OBJECTRRN");
        if (eqpid!= null){
            etEqpWork.setText(eqpid);
            requestCheckInfo(eqpid,"");
        }
    }

    @Override
    protected void initData() {

        initEdit(etEqpWork, s -> {
            requestCheckInfo(s,"");
        });

        initEdit(etLotID, s -> {
            if (entity == null){
                showToast("请先扫描设备",false);
                return;
            }
            //获取lot信息
            LotUtils.searchLotInfo(CheckChangeProductActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    entity.setLOTID(s);
                    tvPackageType.setText(response.getPACKAGETYPE());
                    tvCustomCode.setText(response.getLOT().getCUSTOMERCODE());
                    tvPartName.setText(response.getLOT().getPARTNAME());
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t,"LOTQUERY");
                }

            });
        });

    }

    public void requestCheckInfo(String s,String start){
        FreeUI_EntityCallBack<BaseResponseBean<CheckSheetEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<CheckSheetEntity>>
                (new TypeToken<BaseResponseBean<CheckSheetEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<CheckSheetEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    entity = response.body().getResponse().getBody();
                    if(EqpSubStateMor.equals(entity.getEQPSUBSTATE())){
                        IsStart = true;
                    }

                    if (start.equals(Start)){
                        requestTrackinLogEvent();
                        return;
                    }
                    getEqpStatre(s);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<CheckSheetEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"CHECKSHEET");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERY");
        map.addProperty("EQUIPMENTID",s);
        map.addProperty("CHECKSHEETDATARRN",mObjrctRRN);
        map.addProperty("CHECKTIMING","Daily_ChangeProduct");
        NetUtils.requestPostNet("CPC.CHECKSHEET","CHECKSHEET", CheckChangeProductActivity.this,map,callBack);
    }

    public void requestTrackinLogEvent(){
        FreeUI_EntityCallBack<BaseResponseBean<MLotToolEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MLotToolEntity>>
                (new TypeToken<BaseResponseBean<MLotToolEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("操作成功",true);

                    IsStart = true;
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TRACKINLOGEVENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "TRACKINLOGEVENT");
        map.addProperty("EQUIPMENTID", getText(etEqpWork));
        map.addProperty("COMMENT", "换产点检");
        NetUtils.requestPostNet("CPC.TRACKINLOGEVENT", "TRANSFERMES",this, map, callBack);
    }

    private void getEqpStatre(String s) {
        LotUtils.searchEqpInfo(getText(etEqpWork), CheckChangeProductActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
            @Override
            public void onSuccess(EqpInfoEntity response) {
                if (response.getEQUIPMENT().getSUBSTATE().equals("RUN") || response.getEQUIPMENT().getSUBSTATE().equals("RUN2")){
                    showToast("当前设备处于运行状态",false);
                    entity.setEQUIPMENTID("");
                    etEqpWork.setText("");
                    tvPartName.setText("");
                    tvClazz.setText("");
                    tvEqpType.setText("");
                    tvCheckTime.setText("");
                    tvCheckName.setText("");
                    tvStep.setText("");
                }else {
                    entity.setEQUIPMENTID(getText(etEqpWork));
                    tvPartName.setText(entity.getPARTNAME());
                    tvClazz.setText(entity.getSHIFT());
                    tvEqpType.setText(entity.getEQUIPMENTTYPE());
                    tvCheckTime.setText(entity.getSHEETTIME());
                    if(entity.getSHEETLIST().size() > 0){
                        tvCheckName.setText(entity.getSHEETLIST().get(0).getNAME());
                        tvStep.setText(entity.getSHEETLIST().get(0).getSTEPNAME());
                    }
                }
            }
            @Override
            public void onFail(Throwable t) {
                loadError(t,"EQPQUERY");
            }
        });
    }


    @OnClick({R.id.tvBaseSubmit,R.id.tvState})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (!IsStart){
                    showToast("当前设备未开始",false);
                    return;
                }
                if (actionControl("NewCheck3")){
                    return;
                }
                if (getText(etEqpWork).equals("")){
                    showToast("请扫描设备号",false);
                    return;
                }
                if (entity.getSHEETLIST().size() <= 0){
                    showToast("未获取到点检数据",false);
                    return;
                }
                if (getText(etLotID).equals("")){
                    showToast("请扫描批次号",false);
                    return;
                }

                Bundle bundle = new Bundle();
                bundle.putSerializable("SHEETDATA",entity);
                bundle.putString("OBJECTRRN",mObjrctRRN);
                bundle.putString("EQUIPMENTID",getText(etEqpWork));
                bundle.putString("LOTID",getText(etLotID));
                bundle.putString("CHECKTIME","巡检");
                FreeApi_Utils.skipWithExtraIntent("header",bundle,CheckChangeProductActivity.this,CheckDescActivity.class);
                finish();
                break;
            case R.id.tvState:
                if (getText(etEqpWork).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }
                requestCheckInfo(getText(etEqpWork), Start);
                break;
        }
    }


}
